package com.ihr360.payroll.model.dto.salary.plan;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.ihr360.authority.sharing.entityhandler.UserResourceHandler;
import com.ihr360.commons.entityhandler.DateHandler;
import com.ihr360.commons.entityhandler.Ihr360ContextHandler;
import com.ihr360.payroll.model.dto.attendance.plan.AttendancePlan;
import com.ihr360.payroll.model.dto.attendance.rule.enroll.EnrollAdjustRule;
import com.ihr360.payroll.model.dto.attendance.rule.overtime.OvertimeRule;
import com.ihr360.payroll.model.dto.attendance.rule.personalleave.PersonalLeaveRule;
import com.ihr360.payroll.model.dto.attendance.rule.sickleave.SickLeaveRule;
import com.ihr360.payroll.model.dto.salary.meta.PayrollConfig;
import com.ihr360.payroll.model.vo.AttendancePlanVo;
import com.ihr360.payroll.model.vo.EnrollAdjustRuleVo;
import com.ihr360.payroll.model.vo.MergerTaxPlanVo;
import com.ihr360.payroll.model.vo.OvertimeRuleVo;
import com.ihr360.payroll.model.vo.PayrollConfigVo;
import com.ihr360.payroll.model.vo.PersonalLeaveRuleVo;
import com.ihr360.payroll.model.vo.SickLeaveRuleVo;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

/**
 * 薪酬方案
 *
 * @author tomtao
 */
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Data
@Entity
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"companyId", "name"}))
@EntityListeners(value = {UserResourceHandler.class, Ihr360ContextHandler.class, DateHandler.class})
public class SalaryPlan extends AbstractSalaryPlan {
    public static final class Field {
        public final static String ID = "id";
        public final static String IS_SYSTEM_TEMPLATE = "isSystemTemplate";
        public final static String NAME = "name";
    }

    @Column(length = 40, nullable = false, name = "companyId")
    private String companyId;

    /**
     * 合并计税方案
     */
    @OneToOne
    private SalaryPlan mergerTaxPlan;

    /**
     * 考勤方案
     */
    @ManyToOne(fetch = FetchType.LAZY)
    private AttendancePlan attendancePlan;

    /**
     * 工资表设置
     */
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(nullable = false)
    private PayrollConfig payrollConfig;

    private boolean isSystemTemplate = false;

    public MergerTaxPlanVo getMergerTaxPlanVo() {
        if (mergerTaxPlan != null) {
            return new MergerTaxPlanVo(mergerTaxPlan.getId(), mergerTaxPlan.getName());
        }
        return null;
    }

    @JsonIgnore
    public PayrollConfigVo getPayrollConfigVo() {
        if (payrollConfig != null) {
            return new PayrollConfigVo(payrollConfig.getId(), payrollConfig.getName(), payrollConfig.getFieldVos(), payrollConfig.getSalaryAdjustFormula());
        }
        return null;
    }

    @JsonIgnore
    public AttendancePlanVo getAttendancePlanVo() {
        if (attendancePlan != null) {
            OvertimeRule overtimeRule = attendancePlan.getOvertimeRule();
            OvertimeRuleVo overtimeRuleVo = new OvertimeRuleVo(overtimeRule.getId(), overtimeRule.getWeekendRatio(), overtimeRule.getHolidayRatio(), overtimeRule.getBasementType(), overtimeRule.getFixedBase(),
                    overtimeRule.getBaseRatio(), overtimeRule.getNumOpRatio(), overtimeRule.getRemark(), overtimeRule.getReferenceFields(), null);

            SickLeaveRule sickLeaveRule = attendancePlan.getSickLeaveRule();
            SickLeaveRuleVo sickLeaveRuleVo = new SickLeaveRuleVo(sickLeaveRule.getId(), sickLeaveRule.getBasementType(), sickLeaveRule.getFixedBase(), sickLeaveRule.getBaseRatio(),
                    sickLeaveRule.getNumOpRatio(), sickLeaveRule.getRemark(), sickLeaveRule.getAdvancedDeductionEnable(), sickLeaveRule.getDeductionBy(), sickLeaveRule.getDeductConfig(),
                    sickLeaveRule.getReferenceFields(), sickLeaveRule.getEnableSubsectionPaid());

            PersonalLeaveRule personalLeaveRule = new PersonalLeaveRule();
            PersonalLeaveRuleVo personalLeaveRuleVo = new PersonalLeaveRuleVo(personalLeaveRule.getId(), personalLeaveRule.getBasementType(), personalLeaveRule.getFixedBase(), personalLeaveRule.getBaseRatio(),
                    personalLeaveRule.getNumOpRatio(), personalLeaveRule.getRemark(), personalLeaveRule.getReferenceFields(), personalLeaveRule.getEnableSubsectionPaid());

            EnrollAdjustRule enrollAdjustRule = new EnrollAdjustRule();
            EnrollAdjustRuleVo enrollAdjustRuleVo = new EnrollAdjustRuleVo(enrollAdjustRule.getId(), enrollAdjustRule.getBasementType(), enrollAdjustRule.getFixedBase(), enrollAdjustRule.getBaseRatio(),
                    enrollAdjustRule.getNumOpRatio(), enrollAdjustRule.getRemark(), enrollAdjustRule.getReferenceFields(), enrollAdjustRule.getFormula());
            return new AttendancePlanVo(attendancePlan.getId(), attendancePlan.getName(), attendancePlan.getRemark(), attendancePlan.getWorkHourType(), overtimeRuleVo,
                    attendancePlan.getOvertimeConfig(), sickLeaveRuleVo, attendancePlan.getSickLeaveConfig(), personalLeaveRuleVo,
                    attendancePlan.getPersonalLeaveConfig(),
                    enrollAdjustRuleVo, attendancePlan.getEnrollAdjustConfig(), attendancePlan.getSalaryAdjustConfig());
        }
        return null;
    }

}
